﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Xml.Serialization;
using UpdaterForm.Utils;

namespace UpdaterForm
{ //huinja udalit

    [Serializable]
    public class AppData
    {
        [XmlIgnore] private History _history;
        private String _logFilePath;

        [XmlElement("Name")]
        public String AppName { get; set; }

        [XmlElement("CurrentVersion")]
        public String CurrentVersion { get; set; }

        [XmlElement("CheckSum")]
        public String CheckSum { get; set; }

        [XmlElement("InstallationPath")]
        public String InstallationPath { get; set; }

        [XmlElement("UpdateLogFilePath")]
        public String UpdateLogFilePath
        {
            get
            {
                if (String.IsNullOrEmpty(_logFilePath))
                {
                    _logFilePath = AppName + "Log.xml";
                }
                return _logFilePath;
            }
            set { _logFilePath = value; }
        }


        [XmlIgnore]
        public History History
        {
            get
            {
                if (_history == null)
                {
                    LoadLogs();
                }
                return _history;
            }
        }

        private void LoadLogs()
        {
            var serializer = new XmlSerializer(typeof (History));

            if (!File.Exists(UpdateLogFilePath))
            {
                _history = new History {Logs = new List<UpdateLog>()};
                SaveLogs();
                return;
            }

            using (var reader = new StreamReader(UpdateLogFilePath))
            {
                _history = (History) serializer.Deserialize(reader);
            }
            if (_history == null)
            {
                _history = new History();
            }
        }

        public void SaveLogs()
        {
            var serializer = new XmlSerializer(typeof (History));
            using (var writer = new StreamWriter(UpdateLogFilePath))
            {
                serializer.Serialize(writer, _history);
            }
        }

        public void CalculateSum()
        {
            var md5Array = new List<byte>();
            using (var md5 = MD5.Create())
            {
                foreach (var file in FileUtil.ListAppFiles(InstallationPath))
                {
                    using (var stream = File.OpenRead(file))
                    {
                        md5Array.AddRange(md5.ComputeHash(stream));
                    }
                }

            }
            CheckSum = AppUtil.GetCheckSumm(InstallationPath);
        }

        public bool ControllCheckSumm()
        {
            if (String.IsNullOrEmpty(CheckSum))
                return true;

            return CheckSum.Equals(AppUtil.GetCheckSumm(InstallationPath));
        }
    }
}